package view;

import java.awt.Color;
import java.awt.Graphics;
import java.util.LinkedList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import model.Node;

/**
 * 贪吃蛇的移动面板
 * @author HXZY
 *
 */
public class GamePanel extends JPanel implements Observer{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int WIDTH=500;
	public static final int HEIGHT=500;
	private LinkedList<Node> body;
	private Node food;
	
	public GamePanel() {
		setSize(WIDTH, HEIGHT);
		setBackground(Color.yellow);
		setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
	}
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		if(body==null) return;
		Color color=Color.RED;
		for (Node n : body) {
			g.setColor(color);
			g.fill3DRect(n.getX(), n.getY(), Node.WIDTH, Node.HEIGHT, true);
			color=color.getRGB()==Color.RED.getRGB()?Color.BLACK:Color.RED;	
		
		}
		if(food!=null){
			g.setColor(color.green);
			g.fill3DRect(food.getX(), food.getY(), Node.WIDTH, Node.HEIGHT, true);
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable o, Object arg) {
		if(arg instanceof Node){
			food=(Node) arg;
		}
		else
		{
			body=(LinkedList<Node>) arg;
		}
		repaint();
	}
}
